package com.student.dms;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.luck.picture.lib.utils.SpUtils;
import com.student.dms.databinding.ActivityLoginBinding;
import com.student.dms.databinding.ActivitySplashBinding;
import com.youth.banner.util.LogUtils;

public class SplashActivity extends BaseActivity {
    private static final int EXTERNAL_STORAGE_REQUEST_CODE = 100;
    private static final int MANAGE_APP_REQUEST_CODE = 200;
    private ActivitySplashBinding activitySplashBinding;
    private CountDownTimer countDownTimer;
    private ActivityResultLauncher registerForActivityResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult o) {
            startCountDownTimer();
        }
    });
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activitySplashBinding = ActivitySplashBinding.inflate(getLayoutInflater());
        setContentView(activitySplashBinding.getRoot());
        long time  = 3*1000 ;//定义总时长 2小时
        activitySplashBinding.skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countDownTimer.cancel();
                countDownTimer.onFinish();
            }
        });
        countDownTimer = new CountDownTimer(time, 300) {
            @Override
            public void onTick(long millisUntilFinished) {

            }
            @Override
            public void onFinish() {
                gotoLogin();
            }
        };
        requestIfNotPermision();
    }

    private void requestIfNotPermision() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (Environment.isExternalStorageManager()) {
                startCountDownTimer();
            } else {
                Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                intent.setData(Uri.parse("package:"+getPackageName()));
                registerForActivityResult.launch(intent);
                LogUtils.d("$TAG:    requestMyPermissions SDK_INT >= 30: 没有SD权限");
            }
        }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //判断是否拥有权限
            if (ContextCompat.checkSelfPermission(
                    SplashActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
                    SplashActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE
                ) != PackageManager.PERMISSION_GRANTED  || ContextCompat.checkSelfPermission(
                    SplashActivity.this,android.Manifest.permission.CAMERA
                ) != PackageManager.PERMISSION_GRANTED
            ){
                //没有授权，编写申请权限代码
                ActivityCompat.requestPermissions(
                        SplashActivity.this, new String[]{ android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                                android.Manifest.permission.CAMERA}, EXTERNAL_STORAGE_REQUEST_CODE
                );
                LogUtils.d("$TAG:   requestMyPermissions 23 <= SDK_INT < 30: 没有写SD权限");
            } else {
                startCountDownTimer();
                LogUtils.d("$TAG:  requestMyPermissions 23 <= SDK_INT < 30: 有写SD权限");
            }
        }else {
            startCountDownTimer();
            LogUtils.d("$TAG:   requestMyPermissions: SDK_INT < 23");
        }
    }

    private void startCountDownTimer(){
        countDownTimer.start();
    }
    //判断登录过直接到首页，没登录的话到登录页面
    private void gotoLogin() {
        SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", "");
        if(TextUtils.isEmpty(username)){
            startActivity(new Intent(this,LoginActivity.class));
        }else{
            startActivity(new Intent(this,MainActivity.class));
        }
        finish();
    }
}
